package sketchpad.gui.component;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import sketchpad.business.function.LabelService;
import sketchpad.business.function.impl.LabelServiceImpl;
import sketchpad.constant.CHNName;
import sketchpad.gui.component.utils.ItemIco;
import sketchpad.constant.Name;
import sketchpad.gui.control.DrawControl;

import java.util.logging.Logger;

public class Dialog {

    private static final String FX_BACKGROUND_COLOR = "-fx-background-color: ";
    private static final String FX_TEXT_FILL = "fx-text-fill: ";

    private JFXDialogLayout layout;
    private JFXDialog dialog;
    private JFXComboBox<Label> jfxComboBox;
    private JFXButton jfxButton;
    private DrawControl control;
    private ItemIco itemIco;

    public Dialog(DrawControl drawControl, Stage stage, StackPane pane){
        control = drawControl;
        try {
            itemIco = new ItemIco();
        } catch (Exception e) {
            Logger.getLogger(e.getLocalizedMessage());
        }

        layout = new JFXDialogLayout();
        layout.setMaxWidth(400);
        layout.setMinWidth(400);
        layout.setMinHeight(200);
        layout.setMaxHeight(200);
        dialog = new JFXDialog();
        try {
            addInput();
        } catch (Exception e) {
            Logger.getLogger(e.getLocalizedMessage());
        }
        addButton();
        dialog.setOverlayClose(true);
        dialog.setContent(layout);
        dialog.setDialogContainer(pane);
        dialog.setOnDialogClosed(event -> stage.close());
        HBox hBox = new HBox();
        hBox.getChildren().add(new Label("              "));
        hBox.getChildren().add(jfxComboBox);
        hBox.getChildren().add(new Label("        "));
        hBox.getChildren().add(jfxButton);
        layout.getChildren().add(hBox);
    }

    public void show(String tip){
        layout.setBody(new Label(tip));
        dialog.show();
    }

    private void addInput() throws Exception{
        jfxComboBox = new JFXComboBox<>();
        jfxComboBox.setPromptText("下拉框内选择标签：         ");
        jfxComboBox.setMinWidth(200);
        jfxComboBox.getItems().add(new Label(CHNName.TRIANGLE,itemIco.getTriangle()));
        jfxComboBox.getItems().add(new Label(CHNName.RECTANGLE,itemIco.getRectangle()));
        jfxComboBox.getItems().add(new Label(CHNName.CIRCLE,itemIco.getCircle()));
        jfxComboBox.getItems().add(new Label(CHNName.RANDOM,itemIco.getRandom()));

        jfxComboBox.setConverter(new StringConverter<Label>() {
            @Override
            public String toString(Label object) {
                return object.getText();
            }

            @Override
            public Label fromString(String string) {
                return new Label(string, null);
            }
        });
        jfxComboBox.setOnKeyPressed(event -> {
            if (event.getCode()== KeyCode.ENTER){
                dialog.close();
                writeLabel();
            }
        });
    }

    private void addButton(){
        jfxButton = new JFXButton("确定");
        jfxButton.setStyle(FX_BACKGROUND_COLOR+"#97fff8"+";"+FX_TEXT_FILL+"#fff");
        jfxButton.addEventHandler(MouseEvent.MOUSE_ENTERED,event -> {
            jfxButton.setStyle(FX_BACKGROUND_COLOR+"#2ffdff"+";"+FX_TEXT_FILL+"#fff");
        });
        jfxButton.addEventHandler(MouseEvent.MOUSE_EXITED,event -> {
            jfxButton.setStyle(FX_BACKGROUND_COLOR+"#97fff8"+";"+FX_TEXT_FILL+"#fff");
        });
        jfxButton.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> {
            dialog.close();
            writeLabel();
        });
    }

    private void writeLabel(){
        try {
            String input = jfxComboBox.getValue().getText();
            LabelService labelService = new LabelServiceImpl(control);
            labelService.label(input);
        }catch (Exception ignore){
            Logger.getLogger("放弃选择标记，默认矩形");
        }
    }

    public void disableChooseBox(boolean disable){
        jfxButton.setDisable(disable);
        jfxComboBox.setDisable(disable);
    }

    public void setDefaultItem(String itemName){
        switch (itemName){
            case Name.CIRCLE:
                jfxComboBox.setValue(new Label(CHNName.CIRCLE, itemIco.getCircle()));
                break;
            case Name.RECTANGLE:
                jfxComboBox.setValue(new Label(CHNName.RECTANGLE, itemIco.getRectangle()));
                break;
            case Name.TRIANGLE:
                jfxComboBox.setValue(new Label(CHNName.TRIANGLE, itemIco.getTriangle()));
                break;
            case Name.RANDOM:
                jfxComboBox.setValue(new Label(CHNName.RANDOM, itemIco.getRandom()));
                break;
            default:
        }
    }
}
